#include "ScnsTm1638.c.h"

#if defined(SCNS_TM1638_ENABLE)&&SCNS_TM1638_ENABLE==1

#include "ScnsMath.h"
#include "stm32h750xx.h"
#include "ScnsLed.h"
#include "ScnsKey.h"

const ScnsBspTm1638Config scnsBspTm1638Config[SCNS_TM1638_MAX]={
    {
        .tab=NULL,
#if defined(SCNS_KEY_ENABLE)&&SCNS_KEY_ENABLE==1
        .key={
            SCNS_KEY_S13,SCNS_KEY_S14,SCNS_KEY_S15,SCNS_KEY_S16,SCNS_KEY_S17,SCNS_KEY_MAX,SCNS_KEY_MAX,SCNS_KEY_MAX,
            SCNS_KEY_S7,SCNS_KEY_S8,SCNS_KEY_S9,SCNS_KEY_S10,SCNS_KEY_S11,SCNS_KEY_MAX,SCNS_KEY_MAX,SCNS_KEY_MAX,
            SCNS_KEY_S1,SCNS_KEY_S2,SCNS_KEY_S3,SCNS_KEY_S4,SCNS_KEY_S5,SCNS_KEY_MAX,SCNS_KEY_MAX,SCNS_KEY_MAX,
        },
#endif
    },
    {
        .tab=NULL,
        .reverse=3,
#if defined(SCNS_KEY_ENABLE)&&SCNS_KEY_ENABLE==1
        .key={
            SCNS_KEY_STB0_S1,
            SCNS_KEY_STB0_S2,
            SCNS_KEY_STB0_S3,
            SCNS_KEY_STB0_S4,
            SCNS_KEY_STB0_S5,
            SCNS_KEY_STB0_S6,
            SCNS_KEY_STB0_S7,
            SCNS_KEY_STB0_S8,
            SCNS_KEY_STB0_S9,
            SCNS_KEY_STB0_S10,
            SCNS_KEY_STB0_S11,
            SCNS_KEY_STB0_S12,
            SCNS_KEY_STB0_S13,
            SCNS_KEY_STB0_S14,
            SCNS_KEY_STB0_S15,
            SCNS_KEY_STB0_S16,
            SCNS_KEY_STB0_S17,
            SCNS_KEY_STB0_S18,
            SCNS_KEY_STB0_S19,
            SCNS_KEY_STB0_S20,
            SCNS_KEY_STB0_S21,
            SCNS_KEY_STB0_S22,
            SCNS_KEY_STB0_S23,
            SCNS_KEY_STB0_S24,
        },
#endif
    },
};

ScnsStatus scnsBspTm1638PostInit(ScnsTm1638Enum tm1N)
{
    switch(tm1N)
    {
        case SCNS_TM1638_ON_BOARD:
        {
            for(int i=SCNS_LED_D15;i<=SCNS_LED_D14;++i)
                scnsLedInit(i);
            break;
        }
        case SCNS_TM1638_STB0:
        {
            for(int i=SCNS_LED_STB0_D1;i<=SCNS_LED_STB0_D16;++i)
                scnsLedInit(i);
            break;
        }
        default:;
    }
    return SCNS_STATUS_OK;
}

void scnsBspTm1638SetDioDir(ScnsTm1638Enum tm1N,uint8 in)
{
    const uint32 hb=scnsMathGetHighest1(TM1638_DIO_Pin)*2;
    TM1638_DIO_GPIO_Port->MODER&=~(3<<(hb));
    if(!in)
        TM1638_DIO_GPIO_Port->MODER|=1<<(hb);
}

void scnsBspTm1638SetDio(ScnsTm1638Enum tm1N,uint8 dat)
{
    TM1638_DIO_GPIO_Port->BSRR=(dat)?(TM1638_DIO_Pin):(TM1638_DIO_Pin<<16);
}

uint8 scnsBspTm1638GetDio(ScnsTm1638Enum tm1N)
{
    return ((TM1638_DIO_GPIO_Port->IDR&TM1638_DIO_Pin)?1:0);
}

void scnsBspTm1638SetClk(ScnsTm1638Enum tm1N,uint8 dat)
{
    TM1638_CLK_GPIO_Port->BSRR=(dat)?(TM1638_CLK_Pin):(TM1638_CLK_Pin<<16);
}

void scnsBspTm1638SetStb(ScnsTm1638Enum tm1N,uint8 dat)
{
    switch(tm1N)
    {
        case SCNS_TM1638_ON_BOARD:
        {
            TM1638_STB_GPIO_Port->BSRR=(dat)?(TM1638_STB_Pin):(TM1638_STB_Pin<<16);
            break;
        }
        case SCNS_TM1638_STB0:
        {
            TM1638_STB0_GPIO_Port->BSRR=(dat)?(TM1638_STB0_Pin):(TM1638_STB0_Pin<<16);
            break;
        }
        default:;
    }
}

#endif
